package org.softmed.rest.admin.resource;

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.BoxLayout;
import javax.swing.JPanel;

import main.RESTEditor;

import org.softmed.rest.admin.handler.HandlerChooser;
import org.softmed.rest.admin.httpMethods.MethodActionPanel;
import org.softmed.rest.admin.interfaces.CreateListener;
import org.softmed.rest.admin.interfaces.FileHandler;
import org.softmed.rest.admin.interfaces.SelectionPathChanged;

import restlet.description.convert.RESTResourceDescription;


public class ResourceHandlerChooser extends JPanel implements FileHandler,
		CreateListener, SelectionPathChanged {

	ResourceHandlerPath handlerChooser = new ResourceHandlerPath();
	MethodActionPanel actionPanel = new MethodActionPanel();
	private FileHandler fileHandler;

	public ResourceHandlerChooser() {

		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		setPreferredSize(new Dimension(540, 55));
		setMaximumSize(new Dimension(540, 55));
		setMinimumSize(new Dimension(540, 55));

		actionPanel.setFileHandler(this);
		actionPanel.setCreateListener(this);

		add(handlerChooser);
		add(actionPanel);
		// setBackground(Color.RED);
	}

	@Override
	public void created() {
		String path = RESTEditor.RESTEditor.createFileWithPopup();
		handlerChooser.handlerPath.setText(path);
	}

	@Override
	public String retrieveFilePath() {
		String path = fileHandler.retrieveFilePath();
		if (path.endsWith(".class") || path.endsWith(".java")) {
			// use a FQCN instead of a file path
			int index = path.lastIndexOf('.');
			path = path.substring(1, index);
			path = path.replace('/', '.');
		}

		handlerChooser.handlerPath.setText(path);
		// TODO activate open File button on action Panel
		return null;
	}

	public void setResourceDescription(
			RESTResourceDescription resourceDescription) {
		handlerChooser.setResourceDescription(resourceDescription);

	}

	public void setFileHandler(FileHandler fileHandler) {
		this.fileHandler = fileHandler;

	}

	@Override
	public void selectionPathChanged(boolean directory, String name,
			String parent) {
		actionPanel.selectionPathChanged(directory, name, parent);

	}

}
